package com.zw.pdm.module.craft.controller;

import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.production.*;
import com.zw.pdm.module.basic.service.DictCommonService;
import com.zw.pdm.module.craft.model.ServiceTaskModel;
import com.zw.pdm.module.craft.service.CraftReceiveService;
import com.zw.pdm.module.craft.service.CraftReleaseService;
import com.zw.pdm.module.report.service.ReportQueryService;
import com.zw.pdm.module.report.service.SheetService;
import com.zw.pdm.module.system.service.DeptService;
import com.zw.pdm.module.tecout.service.DrawChangeSheetService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zw.pdm.constant.Constant.GROUP_SPECTYPE;

/**
 * @Description: 计划查询接口
 * @Author: China.sgl
 * @Date: 2020/8/3 16:11
 */
@Controller
@RequestMapping("/craft/receive")
@SuppressWarnings("all")
public class CraftReceiveController extends BaseController {

    @Autowired
    private CraftReceiveService craftReceiveService;
    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private SheetService sheetService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private DrawChangeSheetService drawChangeSheetService;
    @Autowired
    private CraftReleaseService craftReleaseService;

    @GetMapping
    @RequiresPermissions({"tec:proDetail:view"})
    public String page(Model model) {
        model.addAttribute("specialId", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
//        model.addAttribute("depts", deptService.findAllTopLevelDept());
        return "craft/receive/page";
    }

    /**
     * 接收列表
     */
    @ResponseBody
    @PostMapping("/list")
//    @RequiresPermissions({"report:query:view"})
    public JsonResult list(ServiceTaskModel serviceTaskModel) {
        startPage();
        List<ServiceTaskModel> list = craftReceiveService.findWithCraftQuery(serviceTaskModel);
        return jsonResult(list);
    }

    /**
     * 接收列表明细
     */
    @ResponseBody
    @PostMapping("detail/list")
//    @RequiresPermissions({"report:query:view"})
    public JsonResult detailList(Integer sheetId, String sheetType) {
        startPage();
        ServiceTaskModel serviceTaskModel = new ServiceTaskModel();
        serviceTaskModel.setId(sheetId);
        serviceTaskModel.setSheetType(sheetType);
        if (serviceTaskModel.getSheetType().equals("0")) {
            List<DrawChange> drawChangeList = craftReceiveService.SelectDrawChangeBySheetId(serviceTaskModel);
            return jsonResult(drawChangeList);
        } else if (serviceTaskModel.getSheetType().equals("1")) {
            List<MakePlan> makePlanList = craftReceiveService.SelectMakePlanBySheetId(serviceTaskModel);
            return jsonResult(makePlanList);
        } else {
            List<MakeChange> makeChangeList = craftReceiveService.SelectMakeChangeBySheetId(serviceTaskModel);
            return jsonResult(makeChangeList);
        }
    }

    /**
     * 批量接收操作
     */
    @ResponseBody
    @PostMapping("/receiveCard")
    @RequiresPermissions({"tec:batchRecep:edit"})
    public JsonResult receive(String arr1, String arr2) {
        String[] arrId = arr1.split(",");
        String[] arrSheetType = arr2.split(",");
        if (craftReceiveService.receiveDesign(arrId, arrSheetType) > 0) {
            return success();
        } else {
            return JsonResult.fail();
        }
    }


    /**
     * 单个部分接收操作
     */
    @ResponseBody
    @PostMapping("/only/receiveCard")
    @RequiresPermissions({"tec:batchRecep:edit"})
    public JsonResult onlyReceive(String id, String sheetType) {
        String[] arrId = {id.toString()};
        String[] arrSheetType = {sheetType.toString()};
        ServiceTaskModel serviceTaskModel = new ServiceTaskModel();
        serviceTaskModel.setId(Integer.valueOf(id));
        //判断上一单据是否接收，如果没接收提示
        if (sheetType.equals("0") || sheetType.equals(2)) {
        }
        if (sheetType.equals("0")) {
            List<ServiceTaskModel> listDrawSheet = craftReceiveService.findDrawSheetCraft(serviceTaskModel);
            if (listDrawSheet.get(0).getReceiveStatus().equals("1")) {
                return JsonResult.fail();
            }
        } else {
            List<MakeSheet> makeSheetlist = craftReceiveService.selectMakeSheetById(Integer.valueOf(id));
            if (makeSheetlist.size() > 0) {
                if (makeSheetlist.get(0).getReceiveStatus() == 0)
                    return JsonResult.fail();
            }
            List<ServiceTaskModel> listMakeSheet = craftReceiveService.findMakeSheetCraft(serviceTaskModel);
            if (listMakeSheet.get(0).getReceiveStatus().equals("1")) {
                return JsonResult.fail();
            }
        }

        if (craftReceiveService.receiveDesign(arrId, arrSheetType) > 0) {
            return success();
        } else {
            return JsonResult.fail();
        }
    }

    /**
     * 查看操作
     */
    @GetMapping("/info/{id}/{sheetType}")
//    @RequiresPermissions({"report:query:view"})
    public String info(@PathVariable("id") Integer id, @PathVariable("sheetType") Integer sheetType, Model model) {
        ServiceTaskModel serviceTaskModel = new ServiceTaskModel();
        serviceTaskModel.setId(id);
        serviceTaskModel.setSheetType(sheetType.toString());
        List<ServiceTaskModel> list;
        //查看对象 0= 图纸 ，1,2=制作件
        if (sheetType.toString().equals("0")) {
            list = craftReceiveService.findDrawSheetCraft(serviceTaskModel);
        } else {
            list = craftReceiveService.findMakeSheetCraft(serviceTaskModel);
        }
        model.addAttribute("serviceTaskModel", list.get(0));
        if (serviceTaskModel.getSheetType().equals("0")) {
            model.addAttribute("drawChange", drawChangeSheetService.findDrawChangeBySheetIdSelect(id));
            return "craft/receive/drawInfo";
        } else if (serviceTaskModel.getSheetType().equals("1")) {
            List<MakePlan> makePlanList = craftReceiveService.SelectMakePlanBySheetId(serviceTaskModel);
            model.addAttribute("selectAll", makePlanList);
            return "craft/receive/info";
        } else {
            List<MakeChange> makeChangeList = craftReceiveService.SelectMakeChangeBySheetId(serviceTaskModel);
            model.addAttribute("makePlans", makeChangeList);

            return "craft/receive/makeChange";
        }
    }
    /**
     * 查看所有单开页面跳转
     */
    @GetMapping("/only/selectAll")
//    @RequiresPermissions({"tec:batchRecep:edit"})
    public String selectAll(Integer id, String proCode,Model model) {
        ServiceTaskModel serviceTaskModel = new ServiceTaskModel();
        serviceTaskModel.setId(id);
        List<ServiceTaskModel> list = craftReceiveService.findMakeSheetCraft(serviceTaskModel);
        List<MakeResult> makeResultList = craftReceiveService.selectMakeResultByAll(list.get(0));
        model.addAttribute("serviceTaskModel", list.get(0));
        model.addAttribute("makeResultList", makeResultList);
        return  "craft/receive/changeAllDetail";
    }
    /**
     * 查看操作
     */
    @ResponseBody
    @PostMapping("/detail/result")
//    @RequiresPermissions({"report:query:view"})
    public JsonResult result(Integer makeSheetId, Model model) {
        ServiceTaskModel serviceTaskModel = new ServiceTaskModel();
        serviceTaskModel.setId(makeSheetId);
        List<ServiceTaskModel> list = craftReceiveService.findMakeSheetCraft(serviceTaskModel);
        startPage();
        List<MakeResult> makeResultList = craftReceiveService.selectMakeResultByAll(list.get(0));
        return jsonResult(makeResultList);
    }

    /**
     * 查看操作
     */
    @ResponseBody
    @PostMapping("chushihuayuanshi")
//    @RequiresPermissions({"report:query:view"})
    public JsonResult chushihuayuanshi() {
       return jsonResult(craftReleaseService.chushihuayuanshi());
    }
    /**
     * 查看操作
     */
    @ResponseBody
    @PostMapping("chushihua")
//    @RequiresPermissions({"report:query:view"})
    public JsonResult chushihua() {
        return jsonResult(craftReleaseService.chushihua());
    }
}
